﻿#include <Windows.h>
#include <tchar.h>

#include <iostream>

#include "device.h"

using namespace windows::user;

int main() {
  DWORD error;
  Device device;
  WCHAR time_buffer1[24];
  DWORD time_buffer_size1;
  OVERLAPPED overlapped1;

  do {
    error = device.Open(TEXT("Asynchronous"), TRUE);
    if (ERROR_SUCCESS != error) {
      _tprintf(TEXT("main device.Open fail, Error=%d\n"), error);
      break;
    }
    _tprintf(TEXT("device.Open success\n"));

    time_buffer_size1 = sizeof(time_buffer1);
    RtlZeroMemory(&overlapped1, sizeof(overlapped1));
    overlapped1.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (!overlapped1.hEvent) {
      _tprintf(TEXT("First CreateEvent fail, error=%d\n"), GetLastError());
      break;
    }
    error = device.ReadAsync(time_buffer1, time_buffer_size1, overlapped1);
    if (ERROR_SUCCESS != error && ERROR_IO_PENDING != error) {
      _tprintf(TEXT("First device.Read fail, error=%d\n"), error);
      break;
    }
    _tprintf(TEXT("First devoce.Read Success\n"));
    Sleep(1000);
    if (!device.CancelIo()) {
      _tprintf(TEXT("device.CancelIo fail\n"));
      break;
    }
    _tprintf(TEXT("device.CancelIo success\n"));
  } while (false);
  device.Close();
  return EXIT_SUCCESS;
}

